package com.yyx.app1.MedicationAssistant_function.Controls.Medication_Activity_Control.drup_listview;

import android.content.Context;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;


import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by yuexing on 2015/9/7.
 */
public class Medication_ListView_Adapter extends BaseAdapter {
    private ArrayList<HashMap<String,String>> map;
    private Context context;

    public Medication_ListView_Adapter(Context context) {
        this.context = context;
    }

    public void findAll(JSONObject jsonObject)
    {
        map = new ArrayList<>();
        try {
            String[] name = new String[]{"汉语拼音","英文名称","别名","主要成分","性状","制剂与规格","用法用量","药理作用","药物相互作用","适应症","临床应用","不良反应","注意事项","禁忌症"};
            String[] value = new String[name.length];
            value[0]=jsonObject.getString("pinyin");
            value[1]=jsonObject.getString("englishName");
            value[2]=jsonObject.getString("petName");
            value[3]=jsonObject.getString("medicineContent");
            value[4]=jsonObject.getString("shape");
            value[5]=jsonObject.getString("preparation");
            value[6]=jsonObject.getString("functionDosage");
            value[7]=jsonObject.getString("cureFun");
            value[8]=jsonObject.getString("drugInteraction");
            value[9]=jsonObject.getString("indication");
            value[10]=jsonObject.getString("clinical");
            value[11]=jsonObject.getString("effect");
            value[12]=jsonObject.getString("attention");
            value[13]=jsonObject.getString("contraindication");
            for (int i =0 ; i<name.length ;++i)
            {
                HashMap<String,String> one = new HashMap<>();
                one.put("k",name[i]);
                one.put("v",value[i]);
                map.add(i,one);
            }
        }
        catch (Exception e)
        {
            Log.e("异常",e.toString());
        }
    }

    @Override
    public int getCount() {
        return map.size();
    }

    @Override
    public Object getItem(int position) {
        return map.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Medication_ListView_ItemView it;
        if (convertView==null)
        {
            it = Medication_ListView_ItemView_.build(context);
        }
        else
        {
            it = (Medication_ListView_ItemView)convertView;
        }
        it.bind(map.get(position).get("k"), Html.fromHtml(map.get(position).get("v")).toString());
        return it;
    }
}
